package net.edu_soft.ec.asset.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.edu_soft.ec.asset.constant.EduConstant;
import net.edu_soft.ec.asset.mapper.SchoolMapper;
import net.edu_soft.ec.asset.pojo.School;
import net.edu_soft.ec.asset.pojo.SchoolExample;
import net.edu_soft.ec.asset.pojo.vo.SchoolVO;
import net.edu_soft.ec.asset.service.SchoolService;
import net.edu_soft.ec.common.exception.OperationException;
import net.edu_soft.ec.common.util.PuingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class SchoolServiceImpl implements SchoolService {

    @Autowired
    private SchoolMapper schoolMapper;

    @Override
    public School find(Integer schoolId) {
        return selByIdOrName(schoolId,null,1);
    }

    @Override
    public PageInfo<School> findList(String name, Integer pageNum, Integer pageSize) {
        PuingUtil.notNullByInt("高校管理：分页参数不能为空或者小于等于0",pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        SchoolExample example = new SchoolExample();
        SchoolExample.Criteria criteria = example.createCriteria();
        //不为空添加模糊查询条件
        if(name!=null && !"".equals(name)){
            criteria.andNameLike("%"+name+"%");
        }
        List<School> schools = schoolMapper.selectByExample(example);
        PageInfo<School> pageInfo = new PageInfo<>(schools);
        return pageInfo;
    }

    @Override
    public int save(String name, String logo) {
        Assert.hasLength(logo,"-高校管理：高校logo不能为空");
        if(selByIdOrName(0,name,0)!=null){
            throw new OperationException("高校管理，不能重复添加名字为："+name+" 的高校");
        }
        School school  = new School();
        //名称
        school.setName(name);
        //logo
        school.setLogo(logo);
        //状态
        school.setStatus(EduConstant.STATUS_ZREO);
        return schoolMapper.insertSelective(school);
    }

    @Override
    public int update(School school) {
        if(school==null){
            throw new OperationException("高校管理，修改对象为空");
        }
        //根据id查询是否存在
        PuingUtil.notNullByZero(school.getSchoolId(),"高校管理：被修改的高校id不能为空或者小于等于0");
        if (selByIdOrName(school.getSchoolId(),null,1)==null){
            throw new OperationException("高校管理，该高校不存在");
        }
        if(selByIdOrName(null,school.getName(),0)!=null){
            throw new OperationException("高校管理，存在该高校，不能重复添加");
        }
        //防止前端修改为0
        school.setStatus(EduConstant.STATUS_ZREO);
        return  schoolMapper.updateByPrimaryKeySelective(school);
    }

    @Override
    public int del(String ids, Integer isDel) {
        int i = 0;
        //删除
        if(isDel==1){
            //提取id
            List<Integer> list = PuingUtil.getNumber(ids);
            //循环查询
            for (Integer id: list) {
                //查询是否存在
                School school = selByIdOrName(id, null, 1);
                if(school==null){
                    //不存在
                    throw new OperationException("高校管理，该高校id "+id+" 不存在");
                }
                //存在就修改状态为1
                school.setStatus(EduConstant.STATUS_ONE);
                i = schoolMapper.updateByPrimaryKeySelective(school);
            }
        }
        return i;
    }

    @Override
    public PageInfo<SchoolVO> selAllByProductId(Integer productId,Integer pageNum,Integer pageSize,boolean bool) {
        if(bool){
            PuingUtil.notNullByInt("高校管理：查询该产品的所有高校信息，参数不能为空或者小于等于0",pageNum
                    ,pageSize,productId);
        }else {
            PuingUtil.notNullByZero(productId,"高校管理：查询该产品的所有高校信息，产品id不能为空或者小于等于0");
        }
        PageHelper.startPage(pageNum,pageSize);
        List<SchoolVO> schoolVOS = schoolMapper.selAllByProductId(productId);
        PageInfo<SchoolVO> schools = new PageInfo<>(schoolVOS);
        return schools;
    }

    /***
     *
     * 根据id查询高校
     * @param id 高校id
     * @param name 高校名
     * @param flag 0根据名查找，1根据id查找
     * @return
     */
    public School selByIdOrName(Integer id,String name,int flag){
        SchoolExample example = new SchoolExample();
        SchoolExample.Criteria criteria = example.createCriteria();
        if(flag==1){
            PuingUtil.notNullByZero(id,"高校管理：id不能为空或者小于等于0");
            //id
            criteria.andSchoolIdEqualTo(id);

        }else{
            Assert.hasLength(name,"-高校管理：高校名不能为空");
            //name
            criteria.andNameEqualTo(name);
        }
        //状态
        criteria.andStatusEqualTo(EduConstant.STATUS_ZREO);
        List<School> schools = schoolMapper.selectByExample(example);
        if(schools.size()>0){
            return schools.get(0);
        }
        return null;
    }

}
